package com.um.swing.component;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.um.swing.common.util.MathUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseWheelEvent;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;


public class ZPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private String imagePath;
    private float scale = 1f;
    private int startX = -1, startY = -1, endX = -1, endY = -1;

    private int w = 200, h = 200;


    public float getScale() {
        return scale;
    }

    public void setScale(float scale) {
        this.scale = scale;
    }


    public ZPanel() {
        addMouseWheelListener(new MouseAdapter() {
            @Override
            public void mouseWheelMoved(MouseWheelEvent e) {
                super.mouseWheelMoved(e);
                w += e.getWheelRotation() * 10;
                h += e.getWheelRotation() * 10;
                ZPanel.this.updateUI();
            }
        });
    }

    public void setImagePath(String imgPath) {
        // 该方法不推荐使用，该方法是懒加载，图像并不加载到内存，当拿图像的宽和高时会返回-1；
        // image = Toolkit.getDefaultToolkit().getImage(imgPath);
        this.imagePath = imgPath;
        // 该方法会将图像加载到内存，从而拿到图像的详细信息。
        Image image = getImage(imgPath);
        if (image != null) {
            setPreferredSize(new Dimension(1000, 1000));
            image = null;
        }
    }

    @Override
    public void paintComponent(Graphics g1) {
        int x = 0;
        int y = 0;
        Graphics g = (Graphics) g1;
        if (StrUtil.isBlank(this.imagePath)) {
            return;
        }
        Image image = getImage(this.imagePath);
        if (null == image) {
            return;
        }
        Image scaleImg = ImgUtil.scale(image, this.scale);
        image = null;

        Double angle = MathUtils.getAngle(startX, startY, endX, endY);

        int toAngle = Integer.valueOf(90 - angle.intValue());
        if (angle != 0) {
            scaleImg = ImgUtil.rotate(scaleImg, toAngle);
        }
        g.drawImage(scaleImg, x, y, scaleImg.getWidth(this), scaleImg.getHeight(this),
                this);

        g.setColor(Color.RED);
        g.drawString("角度：" + angle + ",矫正角度：" + toAngle, 100, 100);
        g.drawLine(startX, startY, endX, endY);
        g = null;
    }


    private Image getImage(String imagePath) {
        return Toolkit.getDefaultToolkit().getImage(imagePath);
//        try {
//            return ImageIO.read(new FileInputStream(imagePath));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return null;
    }

    public void setLine(int startX, int startY, int endX, int endY) {
        this.startX = startX;
        this.startY = startY;
        this.endX = endX;
        this.endY = endY;
    }
}
